home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c,comp.lang.c++,comp.os.ms-windows.programmer.misc,comp.os.msdos.programmer,comp.programming,comp.windows.ms.programmer
- Subject: Re: Date Arithmetic
- Date: Fri, 16 Feb 96 22:12:21 GMT
- Organization: none
- Message-ID: <824508741snz@genesis.demon.co.uk>
- References: <4g19kp$640@tracy.protocom.com> <3124A458.7BF8@f25mail.amd.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <3124A458.7BF8@f25mail.amd.com>
- carcher@f25mail.amd.com "Clark Archer" writes:
-
- >Michael J. Karas wrote:
- >>
- >> I am working on an algorithm for a laser marking machine that writes
- >> expiration delays on to food product boxes. The algorithm needs to
- >> be able to add NNN days to todays date in the fastest manner possible
- >> without using any floating point arithmetic. I could use help from anyone
- >> that has C code for doing this. It would be nice if the solution took the
- >> leap year problem in to account including the special case of the year
- >> 2000. Thanks in advance to anyone who could share their knowledge on this
- >> subject.
-
- 2000 isn't a special case - it follows the normal Gregorian calendar rules.
-
- >You could use the C runtime library function time() to get the current
- >time and then add NNN * SECS_PER_DAY to it and then convert back to a
- >struct tm like:
-
- The C language does not define the representation of a time_t value, in
- particular it is not guaranteed to represent seconds.
-
- >
- > #define SECS_PER_DAY 86400
- >
- > time_t tmtNow;
- > struct tm tmThen;
- >
- > tmtNow = time(NULL);
- > tmtNow += (nDaysToAdd * SECS_PER_DAY);
- > tmThen = *localtime(tmtNow);
-
- Note that localtime requires a pointer to a time_t value.
-
- What you can do is given suitable tmtNow and nDaysToAdd
-
- tmThen = *localtime(&tmtNow);
- tmThen.tm_mday += NDaysToAdd;
- tmThen.tm_isdst = -1; /* Optional - Allow change of DST status */
- mktime(&tmThen);
-
- mktime sets the members of tmThen correctly and you can also use its return
- value if you want a time_t. To be bullet-proof you should also test for
- failure.
-
- The standard library time.h functions are defined to work according to
- the Gregorian calendar.
-
- This may not be the 'fastest manner possible' but don't assume it is too
- slow before you try it.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-